package com.alstudio.utils.account;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.protocol.xmpp.data.ALPhoto;
import com.alstudio.pojo.module.user.User;
import com.alstudio.pojo.module.user.Voiceintro;
import com.alstudio.utils.android.ALSdcardManager;
import com.alstudio.utils.encode.ALBase64;

public class ALAccountManager {

	private static ALAccount localAccount;

	public static ALAccount loadAccount() {
		// load from database
		if (localAccount == null) {
			localAccount = new ALAccount();
		}
		return localAccount;
	}

	public static void saveAccount(ALAccount account) {
		// save account to database
	}

	/**
	 * 将唯一文件写入sd卡
	 * 
	 * @param name
	 *            文件名
	 * @param trace
	 *            内容
	 */
	public static void makeAccountTrace(String name, String trace) {

		File filey = new File(ALSdcardManager.getExtStoragePath() + "/." + name);
		if (!filey.exists()) {
			try {
				filey.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(filey));
			String temp = ALBase64.encode(trace.getBytes());

			bw.write(temp);
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 检测指定文件中是否数据
	 * 
	 * @param name
	 * @return
	 */
	public static String checkAccountTrace(String name) {
		if (TextUtils.isEmpty(name))
			return null;
		File filex = new File(ALSdcardManager.getExtStoragePath() + "/." + name);
		BufferedReader br = null;
		String trace = "";
		try {
			br = new BufferedReader(new FileReader(filex));
			trace = br.readLine();
			trace = new String(ALBase64.decode(trace));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (br != null) {
					br.close();
				}
				filex = null;
				br = null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return trace;
	}

}
